<template>
  <div class="login-container">
    <!-- 顶部导航 -->
    <div class="top_nav">
      <div class="nav-icon" @click="toggleLanguage">
        <van-icon name="globe-o" class="globe-icon" />
      </div>
      <div class="nav-icon">
        <img src="../assets/my_right.png" alt="" />
      </div>
    </div>

    <!-- 登录框 -->
    <div class="login-box">
      <div class="logo">{{ $t("login.title") }}</div>

      <van-form @submit="onSubmit">
        <!-- 邮箱输入框 -->
        <van-field
          v-model="email"
          name="email"
          type="email"
          :placeholder="$t('login.email')"
          :rules="[{ required: true, message: $t('login.emailRequired') }]"
        >
          <template #left-icon>
            <van-icon name="envelop-o" class="email-icon" />
          </template>
        </van-field>

        <!-- 密码输入框 -->
        <van-field
          v-model="password"
          name="password"
          :type="showPassword ? 'text' : 'password'"
          :placeholder="$t('login.password')"
          :rules="[{ required: true, message: $t('login.passwordRequired') }]"
        >
          <template #left-icon>
            <van-icon name="lock" />
          </template>
          <template #right-icon>
            <van-icon
              :name="showPassword ? 'eye-o' : 'closed-eye'"
              class="eye-icon"
              @click="togglePassword"
            />
          </template>
        </van-field>

        <!-- 记住密码 -->
        <van-field name="checkboxGroup" label="">
          <template #input>
            <van-checkbox-group
              v-model="rememberPassword"
              direction="horizontal"
            >
              <van-checkbox class="small-checkbox" name="1" shape="square">{{
                $t("login.rememberPassword")
              }}</van-checkbox>
            </van-checkbox-group>
          </template>
        </van-field>

        <!-- 登录按钮 -->
        <div style="margin: 16px">
          <van-button
            round
            block
            type="primary"
            native-type="submit"
            class="login-btn"
          >
            {{ $t("login.login") }}
          </van-button>
        </div>
      </van-form>

      <!-- 注册链接 -->
      <div class="register-link">
        {{ $t("login.register") }}
        <a href="/register">{{ $t("login.toRegister") }}</a>
      </div>
    </div>
  </div>
</template>

<script setup name="Login">
import { ref, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import { Icon, Form, Field, Checkbox, Button, Toast } from "vant";
import { useRouter } from "vue-router";
import { UserApi } from "@/api/user.api";
import { useUserStore } from "@/stores/user";
const userStore = useUserStore();
const router = useRouter();
const { t, locale } = useI18n();

const email = ref("");
const password = ref("");
const rememberPassword = ref([]);
const showPassword = ref(false);

const togglePassword = () => {
  showPassword.value = !showPassword.value;
};

const toggleLanguage = () => {
  console.log(locale.value, 111);
  const newLocale = locale.value === "zh" ? "en" : "zh";
  locale.value = newLocale;
  localStorage.setItem("locale", newLocale);
};

const onSubmit = (values) => {
  Toast(t("login.loggingIn"));
  console.log("submit", values);
  if (rememberPassword.value.includes("1")) {
    // 保存邮箱和密码到 localStorage
    localStorage.setItem("savedEmail", email.value);
    localStorage.setItem("savedPassword", password.value);
    localStorage.setItem("isRemember", "true");
  } else {
    // 清除保存的信息
    localStorage.removeItem("savedEmail");
    localStorage.removeItem("savedPassword");
    localStorage.removeItem("isRemember");
  }
  // 这里添加登录逻辑
  const loginData = {
    email: email.value,
    password: password.value,
  };
  UserApi.login(loginData).then((res) => {
    console.log(res);
    if (res.code == 200) {
      Toast(t("login.loginSuccess"));
      userStore.setToken(res.data.token);
      router.push("/home");
    } else {
      Toast(t("login.loginFailed"));
    }
  });
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  padding: 20px;
  box-sizing: border-box;
}
.top_nav {
  position: absolute;
  top: 20px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}
.nav-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.nav-icon img {
  width: 24px;
  height: 24px;
  object-fit: contain;
}
.globe-icon {
  width: 24px;
  height: 24px;
  background: url(../assets/my_left.svg) center / contain no-repeat;
}
.service-icon {
  width: 24px;
  height: 24px;
  object-fit: contain;
}
.login-box {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 16px;
  padding: 30px 20px;
  box-shadow: 0 2px 12px #0000000a;
}
.logo {
  text-align: center;
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 30px;
}
.form-item {
  margin-bottom: 20px;
}
.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border-radius: 25px;
  padding: 12px 15px;
}
.register-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}
.register-link a {
  color: #4460f6;
  text-decoration: none;
}
:deep(.small-checkbox .van-checkbox__icon) {
  width: 16px;
  height: 16px;
}
:deep(.small-checkbox .van-checkbox__icon .van-icon) {
  font-size: 12px;
}
</style>
